package com.wg.wgaiagent.manager;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static com.wg.wgaiagent.constant.FileConstant.FILE_SAVE_DIR;

/**
 * @author with葡萄成熟时
 * @version 1.0
 * 保存md文件到本地
 */
@Component
public class UploadMdFile {

    // 文件保存路径，实际应用中建议配置在配置文件中
    private static final String UPLOAD_DIR = FILE_SAVE_DIR;


    public String uploadMdFile(MultipartFile file) {
        String fileName = file.getOriginalFilename();


        try {
            // 创建上传目录（如果不存在）
            File uploadDir = new File(UPLOAD_DIR);
            if (!uploadDir.exists()) {
                uploadDir.mkdirs();
            }

            // 构建文件保存路径
            Path filePath = Paths.get(UPLOAD_DIR + fileName);

            // 保存文件到本地
            Files.write(filePath, file.getBytes());

            return fileName;
        } catch (IOException e) {

            return null;
        }
    }
}
